package com.its.oa.system.module.dao.impl;

import java.io.Serializable;
import java.util.List;

import com.its.oa.dao.base.ItsHibernateDaoSupport;
import com.its.oa.model.Module;
import com.its.oa.model.Role;
import com.its.oa.system.module.dao.ModuleDao;
import com.its.oa.util.EntityValidata;
import com.its.oa.util.StringUtil;

public class ModuleDaoImpl extends ItsHibernateDaoSupport implements ModuleDao {

	public String add(Module module) {
		Serializable sl =getHibernateTemplate().save(module);
		return sl.toString();
	}

	public String delete(Module module) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Module> findAll() {
		List<Module> list = getHibernateTemplate().find("from Module");
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Module> findByCode(String code) {
		List<Module> list =getHibernateTemplate().find("from Module where code like '%"+code+"%'");
		if(EntityValidata.isNotEmpty(list))
			return list;
		else
			return null;
	}

	public List<Module> findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public String update(Module module) {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Module> findModulesByRoles(List<Role> roleList){
		String moduleCodes = "";
		for (Role role : roleList) {
			
			moduleCodes+=role.getModCode();
		}
		moduleCodes = StringUtil.removeSame(moduleCodes);
		moduleCodes = StringUtil.formatStrArryToStr(moduleCodes);
		String hql = "from Module where code in ("+moduleCodes+")";
		List<Module> moduleList = getHibernateTemplate().find(hql);
		return moduleList;
	}

	@SuppressWarnings("unchecked")
	public List<Module> findAllByPage(int offset, int pagesize) {
		List<Module> list = (List<Module>) findByPage("from Module ", offset, pagesize);
		return list;
	}

	@SuppressWarnings("unchecked")
	public Module findById(int id) {
		List<Module> list = getHibernateTemplate().find("from Module m where m.id="+id);
		if(EntityValidata.isNotEmpty(list)){
			return list.get(0);
		}else
			return null;
	}
}
